#!/usr/bin/perl -w

BEGIN {
	push @INC,"../lib";
}

use Data::Dumper;
use ipinterfaces;
use strict;

$::ip=ipinterfaces->new();
if(!defined($::ip)) {
	print STDERR "Error - Unable to use ipinterfaces module! - 001\n";exit(1);
}
print "001: Ipinterfaces object defined successfully.\n";

eval {local $SIG{__DIE__}; @::l=$::ip->list(physical=>1);};
if($@ || !defined(@::l)) {
	print STDERR "Error - Unable to list physical interfaces - 002\n";exit(1);
}
print "002: Ipinterfaces physical interfaces: ",join(",",@::l),"\n";

eval {local $SIG{__DIE__}; @::l2=$::ip->list(logical=>1);};
if($@) {
	print STDERR "Error - Unable to list logical interfaces - 003\n";exit(1);
}
print "003: Ipinterfaces logical interfaces: ",join(",",@::l2),"\n";

$::i=0;
while(1) {
	if($::i>scalar(@::l)) {
		print STDERR "Error - No cards found with ip addresses!\n";exit(1);
	}
	$::ref=$::ip->get_details(interface=>"$::l[$::i]");
	if($::ref && $::ref->{ip}) {
		last;
	}
	$::i++;
}

eval {local $SIG{__DIE__}; $::r=$::ip->get_logical(interface=>$::l[$::i]);};
if($@ || !defined(@::l)) {
	print STDERR "Error - Unable to get next logical interface for $::l[$::i] - 004\n";exit(1);
}
print "004: Next logical interface for $::l[$::i] is $::l[$::i]:$::r\n";

$::ref=$::ip->get_details(interface=>"$::l[$::i]");
if(!defined($::ref)) {
	print STDERR "Error - Unable to get details for interface $::l[$::i]:0 - 005\n";exit(1);
}
print "005: Details for $::l[$::i] are:\n";
print "     IP=$::ref->{ip},NETMASK=>$::ref->{netmask},BROADCAST=$::ref->{broadcast}\n";

$::phys=$::l[$::i];
@::R=$::ip->get_routes(interface=>$::phys);
print "006: Routes associated with $::phys :\n";
print "     $_\n" foreach(@::R);

$::int=$::ip->get_interface(ip=>$::ref->{ip});
if(!defined($::int)) {
	print STDERR "Error - Unable to cross reference IP to interface - 007\n";exit(1);
}
print "007: Interface for IP address $::ref->{ip} is '$::int'.\n";

